package test;

import cn.xdd.dao.BookDao;
import cn.xdd.entity.Book;
import com.google.gson.Gson;
import org.apache.log4j.Logger;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.List;

/**
 * @author user_xdd
 * @Date 2022-04-04 10:27
 * @description
 * @return
 */
@WebServlet(urlPatterns = {"/testbook"})
public class file extends GenericServlet {
    private static final Logger LOGGER = Logger.getLogger(file.class);

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        request.setCharacterEncoding("utf-8");//设置请求中文编码
        response.setContentType("text/html;charset=utf-8");//设置响应中文编码

        String Path = request.getServletContext().getRealPath("/") + "data/book";
        BookDao bookDao = new BookDao();
        File file;

//        为所有未创建图书文件的图书创建目录 并且每个图书创建三个章节内容问章节名字
        try {
            List<Book> books = bookDao.getallBook();
            for (Book b : books) {
                String bookPaths = Path + File.separator + b.getBook_address();
                LOGGER.debug("    bookPaths:" + bookPaths);
                file = new File(bookPaths);
//                 文件夹的创建
                if (!file.exists()) {
                    file.mkdir();
                }
//                创建文件
                for (int i = 1; i <= 3; i++) {
                    String CName = String.format("%05d", i);
                    CName = CName + "这里是第" + i + "章节.txt";

                    String CPaths = bookPaths + File.separator + CName;
                    LOGGER.debug("    CPaths:" + CPaths);
                    file = new File(CPaths);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
//                    写入数据
                    String msg = CName;
                    byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
                    FileOutputStream in = new FileOutputStream(file);
                    in.write(bytes, 0, bytes.length);
                   //------------------------------- -------------------------------
/*                    String readMsg = null;
                    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

                    String s = null;
                    while ((s = bufferedReader.readLine()) != null) {//使用readLine方法，一次读一行
                        readMsg = readMsg + "\n" + s;
                    }
                    bufferedReader.close();

                    Gson gson=new Gson();
                    String json = gson.toJson(readMsg);
                    response.getWriter().println(json);
                            */

                }
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }

//打印章节名称
/*        ArrayList<String> list = new ArrayList<String>(0);//用arraylist保存扫描到的路径

        File file = new File(Path);
        File[] files = file.listFiles();
        LOGGER.debug("    files.toString():"+files.toString());



        String[] filenames = file.list();
//        检查是不是文件
        if (filenames == null)
            return;
        for (int i = 0; i < filenames.length; i++) {
            if (files[i].isFile()) {
                if (files[i].getName().endsWith(".txt"))//只获取带png结尾的文件
//                    list.add(files[i].getPath());//获取路径
                    list.add(filenames[i]);
            }
        }
        LOGGER.debug("    list:"+list);*/

    }
}
